package com.yxch.admin.service.impl.comment;

import com.yxch.admin.mapper.CommenMapper;
import com.yxch.admin.mapper.comment.CommentMapper;
import com.yxch.admin.model.commen.AuditLog;
import com.yxch.admin.model.commen.PageResult;
import com.yxch.admin.model.comment.*;
import com.yxch.admin.service.comment.CommentService;
import com.yxch.admin.utils.ResultAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private CommenMapper commenMapper;

    @Override
    public ResultAPI getPosts(PostDTO postDTO) {
        Integer pageNum = postDTO.getPageNum();
        Integer pageSize = postDTO.getPageSize();
        String postTitle = postDTO.getPost_title();
        String postType = postDTO.getPost_type();

        Integer start = (pageNum - 1) * pageSize;
        PageResult pageResult = commentMapper.getPosts(start, pageSize, postType, postTitle);
        return ResultAPI.success(pageResult);
    }

    @Override
    public ResultAPI getPostById(Integer postId) {
        PostVO postVO = commentMapper.getPostById(postId);
        return ResultAPI.success(postVO);
    }

    @Transactional
    @Override
    public ResultAPI postAudit(AuditLog auditLog) {
        commentMapper.updatePost(auditLog);
        if (auditLog.getLog_id() != null) {
            commenMapper.updateAudit(auditLog);
        } else {
            commentMapper.insertAudit(auditLog);
        }
        return ResultAPI.success();
    }

    @Override
    public ResultAPI getBanword(Integer pageSize, Integer pageNum) {
        Integer start = (pageNum - 1) * pageSize;
        PageResult pageResult = commentMapper.getBanword(start, pageSize);
        System.out.println(pageResult);
        return ResultAPI.success(pageResult);
    }

    @Override
    public ResultAPI getComments(CommentDTO commentDTO) {
        Integer pageNum = commentDTO.getPageNum();
        Integer pageSize = commentDTO.getPageSize();
        Integer start = (pageNum - 1) * pageSize;
        commentDTO.setStart(start);
        PageResult res = commentMapper.getComments(commentDTO);
        return ResultAPI.success(res);
    }

    @Override
    public ResultAPI addorUpdateBanword(BanWord banWord) {
        if (banWord.getId() != null) {
            commentMapper.updateBanword(banWord);
        } else {
            commentMapper.addBanword(banWord);
        }
        return ResultAPI.success();
    }

    @Override
    public ResultAPI deleteBanword(List<Integer> ids) {
        commentMapper.deleteBanword(ids);
        return ResultAPI.success();
    }

    @Override
    public ResultAPI deleteComments(List<Integer> ids) {
        commentMapper.deleteComments(ids);
        return ResultAPI.success();
    }

    @Override
    public ResultAPI updateTop(PostTop postTop) {
        commentMapper.updateTop(postTop);
        return ResultAPI.success();
    }

    @Override
    public void addPost(PostAdd postAdd) {
        commentMapper.addPost(postAdd);
    }

    @Override
    public void deletePost(List<Integer> ids) {
        commentMapper.deletePosts(ids);
    }
}
